<template>
    <div style="margin-top: -10px">
        <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline">
            <el-form-item label="关键字">
                <el-input v-model="searchForm.keyword" placeholder="关键字（标题或地址）"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-select v-model="searchForm.state"  clearable placeholder="订单状态">
                    <el-option label="待接单" value="0"></el-option>
                    <el-option label="已接单" value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="发布时间">
                <el-date-picker
                        v-model="searchForm.betweenTime"
                        type="daterange"
                        value-format="yyyy-MM-dd"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="toQuery">查询</el-button>
                <el-button type="danger" @click="patchDel">批量删除</el-button>
            </el-form-item>
        </el-form>
        <el-table
                :data="tableData"
                border
                style="width: 100%;"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    prop="resourcesStr"
                    label="宠物图片">
                <template slot-scope="scope">
                    <el-image :src="scope.row.resources"
                              :preview-src-list="[scope.row.resources]"></el-image>
                </template>
            </el-table-column>
            <el-table-column
                    prop="userName"
                    label="售主">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="宠物名字">
            </el-table-column>
            <el-table-column
                    prop="petType"
                    label="宠物类型">
            </el-table-column>
            <el-table-column
                    prop="state"
                    label="状态" >
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.state == 0" type="info">待接单</el-tag>
                    <el-tag v-if="scope.row.state == 1" type="warning">已接单</el-tag>
                    <el-tag v-if="scope.row.state == 2" type="warning">待收购</el-tag>
                    <el-tag v-if="scope.row.state == 3" type="warning">已完成</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="adoptName"
                    label="收购员">
            </el-table-column>
            <el-table-column
                    prop="msgId"
                    label="订单号">
            </el-table-column>
            <el-table-column
                    prop="payType"
                    label="支付类型">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.payType == 0" type="info">垫付</el-tag>
                    <el-tag v-if="scope.row.payType == 1" type="warning">余额转账</el-tag>
                    <el-tag v-if="scope.row.payType == 2" type="warning">银行转账</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="price"
                    label="价格">
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="收购公司地址">
            </el-table-column>

            <el-table-column label="操作" width="150px">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.state == 0"
                               size="mini"
                               type="success"
                               @click="handlerShenghe(scope.row)">接单操作
                    </el-button>
                    <el-button v-if="scope.row.state == 2"
                               size="mini"
                               type="success"
                               @click="handlerShenghe(scope.row)">收购操作
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                style="margin-top: -10px"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="searchForm.page"
                :page-sizes="[5, 10, 15, -10]"
                :page-size="searchForm.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>


        <el-dialog title="订单操作" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="标题" label-width="100px">
                    <el-input v-model="form.title" disabled></el-input>
                </el-form-item>
                <el-form-item label="订单备注" label-width="100px">
                    <el-input
                            type="textarea"
                            placeholder="请输入内容"
                            v-model="form.note"
                            maxlength="-100"
                            show-word-limit
                    >
                    </el-input>
                </el-form-item>

                <el-form-item label="分配员工" label-width="100px">
                    <el-select v-model="form.empId" clearable placeholder="请选择">
                        <el-option
                                v-for="item in shopEmployees"
                                :key="item.id"
                                :label="item.username"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="shenghe(1)">接单</el-button>
                <el-button @click="dialogFormVisible=false">取消</el-button>
            </div>
        </el-dialog>
<!--收购-->
        <el-dialog title="收购操作" :visible.sync="adoptFormVisible">
            <el-form :model="form">
                <el-form-item label="收购者">
                    <el-input v-model="form.adoptName"></el-input>
                </el-form-item>
                <el-form-item label="售主">
                    <el-input v-model="form.userName"></el-input>
                </el-form-item>
                <el-form-item label="收购公司地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
                <el-form-item label="宠物名字名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="收购价格">
                    <el-input v-model="form.price"></el-input>
                </el-form-item>

                <el-form-item label="宠物备注">
                    <el-input v-model="form.digest"></el-input>
                </el-form-item>
                <el-form-item label="宠物图片">
                    <template>
                        <el-image :src="form.resources"></el-image>
                    </template>
                </el-form-item>
                <el-form-item label="宠物类型">
                    <el-input v-model="form.petType"></el-input>
                </el-form-item>
                <el-form-item label="支付方式">
                    <el-select v-model="form.payType" clearable placeholder="请选择支付方式">
                        <el-option label="垫付" value=0></el-option>
                        <el-option label="余额支付" value=1></el-option>
                        <el-option label="银行转账" value=2></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="adoptshenghe(3)">确认收购</el-button>
                <el-button @click="notadopt()">取消收购</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "PetShengHe",
        data() {
            return {
                form: { //下面的3个属性，可省略
                    id: '',//审核的数据的主键 ID
                    title: '', //显示的标题
                    note: '', //备注,
                    state: null ,//审核状态
                    adoptName:'',
                    userName:'',
                    digest:'',
                },
                shopEmployees:[],
                dialogFormVisible: false,
                adoptFormVisible:false,
                total: 0, //总记录数
                multipleSelection: [],
                searchForm: {
                    page: 1,
                    size: 10,
                },
                tableData: []
            }
        },
        methods: {
            adoptshenghe(state) {
                this.form.state = state;
                this.$http.post('/order/adopt/', this.form).then(res => {
                    let {message, success} = res.data;
                    if (success) {
                        this.adoptFormVisible = false;
                        this.toQuery();
                        this.$message.success(message);
                    } else {
                        this.$message.error(message);
                    }
                })
            },
            notadopt(){
                //$refs调用js 原生dom元素
                //validate
                this.$confirm('确认取消吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    // let para = {ids: ids};
                    // console.log(ids)
                    this.$http.post("/order/cancel",this.form).then((res) => {
                        console.log(res)
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message:res.data.message,
                            type: 'success'
                        });
                        this.adoptFormVisible = false;
                        this.toQuery();
                    });
                }).catch(() => {

                });
            },
            toQuery() {
                if (this.searchForm.betweenTime && this.searchForm.betweenTime.length > 0) {//判断参数非空（!= '' && != underfind && != NAN）
                    console.log(this.searchForm.betweenTime);
                    this.searchForm.startTime=this.searchForm.betweenTime[0]
                    this.searchForm.endTime=this.searchForm.betweenTime[1]
                }
                console.log("选择的时间：" + JSON.stringify(this.searchForm));
                this.$http.post("/order/findPage", this.searchForm).then(res => {
                    this.tableData = res.data.data.list;
                    this.total = res.data.data.total;
                })
            },
            handleEdit(index, row) {
                console.log(index, row);
            },
            handlerShenghe(row) {
                console.log("收购信息")
                console.log(row);
                this.adoptFormVisible = true;
                //TODO 下面的这个操作 ，是直接当前这一整行的数据，赋值给了form
                //而form对应在后台实体类的 TSearchMasterMsg对象
                this.form = row;
             //   this.form.title = row.title;
             //   this.form.id = row.id;

            },
            handleSizeChange(val) {
                this.searchForm.size = val;
                this.toQuery();
            },
            handleCurrentChange(val) {
                this.searchForm.page = val;
                this.toQuery();
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            findShopEmployee(){
                this.$http.get("/emp/findShopAllEmployee").then(res => {
                    this.shopEmployees = res.data.data
                })
            }
        },
        mounted() {
            this.toQuery();
            this.findShopEmployee();
        }
    }
</script>

<style scoped>

</style>
